package com.carassistant.beta.common;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;

/***
 * 有关手机信息获取类
 * 
 * @author james
 * 
 */
public class SystemSetHelper {

	private static Context mContext = null;
	private static TelephonyManager telephonyManager;
	/**
	 * 国际移动用户识别码
	 */
	private String IMSI;

	public static final void initContext(Context context) {
		mContext = context;
		telephonyManager = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
	}

	/**
	 * 判断网络是否打开
	 */
	public static boolean isNetwrokOpened() {
		boolean isconnect = false;
		ConnectivityManager conManager = (ConnectivityManager) mContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo network = conManager.getActiveNetworkInfo();
		if (network != null) {
			Log.i("网络名称", network.getTypeName() + "代码：" + network.getType());
			if (network.getType() == ConnectivityManager.TYPE_MOBILE
					|| network.getType() == ConnectivityManager.TYPE_WIFI) {
				isconnect = true;
			} else {
				isconnect = false;
			}
		} else {
			isconnect = false;
		}
		return isconnect;
	}

	/**
	 * 获取网络状态
	 */
	public static String getNetwrokState() {
		String netState = "";
		ConnectivityManager conManager = (ConnectivityManager) mContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo network = conManager.getActiveNetworkInfo();
		if (network != null) {
			netState = network.getState().toString();
		} else {
			netState = NetworkInfo.State.DISCONNECTED.toString();
		}
		return netState;
	}

	/**
	 * 判断GPS是否开启
	 * 
	 * @return
	 */
	public static boolean isGpsOpened() {
		boolean isopen = false;
		LocationManager locMag = (LocationManager) mContext
				.getSystemService(Context.LOCATION_SERVICE);
		if (locMag.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			isopen = true;
		}
		return isopen;
	}
/***
 * 
 * @return
 */
	public static boolean enableGpsProvider() {
		boolean isSuccess = false;
		Intent gpsIntent = new Intent();
		gpsIntent.setClassName("com.android.settings",
				"com.android.settings.widget.SettingsAppWidgetProvider");
		gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
		gpsIntent.setData(Uri.parse("custom:3"));
		try {
			PendingIntent.getBroadcast(mContext, 0, gpsIntent, 0).send();
		} catch (Exception e) {
			e.printStackTrace();
		}
		LocationManager locMag = (LocationManager) mContext
				.getSystemService(Context.LOCATION_SERVICE);
		if (locMag.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			isSuccess = true;
		}
		return isSuccess;
	}

	/**
	 * 获取本机电话号码
	 */
	public String getNativePhoneNumber() {
		String mobilePhone = null;
		mobilePhone = telephonyManager.getLine1Number();
		return mobilePhone;
	}

	/**
	 * 获取手机服务商信息
	 */
	public String getProvidersName() {
		String ProvidersName = "N/A";
		try {
			IMSI = telephonyManager.getSubscriberId();
			// IMSI号前面3位460是国家，紧接着后面2位00 02是中国移动，01是中国联通，03是中国电信。
			System.out.println(IMSI);
			if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
				ProvidersName = "中国移动";
			} else if (IMSI.startsWith("46001")) {
				ProvidersName = "中国联通";
			} else if (IMSI.startsWith("46003")) {
				ProvidersName = "中国电信";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ProvidersName;
	}

	/**
	 * 获取外置SD卡路径
	 * 
	 * @return
	 */
	public static String getSDCardPath() {
		String cmd = "cat /proc/mounts";
		Runtime run = Runtime.getRuntime();// 返回与当前 Java 应用程序相关的运行时对象
		try {
			Process p = run.exec(cmd);// 启动另一个进程来执行命令
			BufferedInputStream in = new BufferedInputStream(p.getInputStream());
			BufferedReader inBr = new BufferedReader(new InputStreamReader(in));

			String lineStr;
			while ((lineStr = inBr.readLine()) != null) {
				// 获得命令执行后在控制台的输出信息
				if (lineStr.contains("sdcard")
						&& lineStr.contains(".android_secure")) {
					String[] strArray = lineStr.split(" ");
					if (strArray != null && strArray.length >= 5) {
						String result = strArray[1].replace("/.android_secure",
								"");
						return result;
					}
				}
				// 检查命令是否执行失败。
				if (p.waitFor() != 0 && p.exitValue() == 1) {
					// p.exitValue()==0表示正常结束，1：非正常结束
				}
			}
			inBr.close();
			in.close();
		} catch (Exception e) {

			return Environment.getExternalStorageDirectory().getPath();
		}

		return Environment.getExternalStorageDirectory().getPath();
	}

	/****
	 * 手机可用空间大小
	 * 
	 * @return
	 */
	static public long getAvailableInternalMemorySize() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		return availableBlocks * blockSize;
	}

	/****
	 * sd卡可用空间大小
	 * 
	 * @return
	 */

	static public long getAvailableExternalMemorySize() {
		File path = Environment.getExternalStorageDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		return availableBlocks * blockSize;

	}
}
